/*
 *      Copyright 2008 Battams, Derek
 *       
 *       Licensed under the Apache License, Version 2.0 (the "License");
 *       you may not use this file except in compliance with the License.
 *       You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *       Unless required by applicable law or agreed to in writing, software
 *       distributed under the License is distributed on an "AS IS" BASIS,
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *       See the License for the specific language governing permissions and
 *       limitations under the License.
 */
package com.google.code.gwtsrwc.client;

/**
 * A concrete implementation of ValidatedTextBox that validates integers
 * @author $Author: derek@battams.ca $
 * @version $Id: ValidatedIntegerTextBox.java 11 2008-09-04 00:43:06Z derek@battams.ca $
 * $URL: http://gwtsrwc.googlecode.com/svn/trunk/src/com/google/code/gwtsrwc/client/ValidatedIntegerTextBox.java $
 */
public class ValidatedIntegerTextBox extends ValidatedTextBox {
	static private class IntegerValidator extends TextBoxValidator {
		private int low, high;
		
		public IntegerValidator(int low, int high) {
			this.low = low;
			this.high = high;
		}
		
		@Override
		public boolean isValueValid(String data) {
			int val;
			
			try {
				val = Integer.parseInt(data);
			} catch(NumberFormatException e) {
				setLastError(WidgetStrings.constants.mustBeInt());
				return false;
			}
			
			if(val < low || val > high) {
				setLastError(WidgetStrings.messages.mustBeInRange(Integer.toString(low), Integer.toString(high)));
				return false;
			}
			else {
				setLastError(null);
				return true;
			}
		}
	}
	
	private int low;
	private int high;
	
	/**
	 * Constructor
	 * @param low The lowest possible integer this input will accept; use Integer.MIN_VALUE to ignore the lower limit (except for that imposed by the JRE)
	 * @param high The highest possible integer this input will accept; use Integer.MAX_VALUE to ignore the upper limit (except for that imposed by the JRE)
	 */
	public ValidatedIntegerTextBox(int low, int high)
	{
		super(new IntegerValidator(low, high));
		this.low = low;
		this.high = high;
	}
	
	/**
	 * Get the lowest value this input will accept
	 * @return The lowest value this input will accept
	 */
	final public int getLow()
	{
		return low;
	}
	
	/**
	 * Get the highest value this input will accept
	 * @return The highest value this input will accept
	 */
	final public int getHigh()
	{
		return high;
	}	
}